package Q5;

import java.util.Scanner;

public class Question5 {
    static int[][] allocations;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取资源总量
        int[] totalResources = new int[3]; // A, B, C 三种资源
        totalResources[0] = scanner.nextInt();
        totalResources[1] = scanner.nextInt();
        totalResources[2] = scanner.nextInt();

        // 读取进程信息
        String[] processNames = new String[4];
        int[][] claims = new int[4][3]; // 需要的资源
        allocations = new int[4][3]; // 已分配的资源
        int[][] needs = new int[4][3]; // 需要的资源量 (Claim - Allocation)

        for (int i = 0; i < 4; i++) {
            processNames[i] = scanner.next();
            for (int j = 0; j < 3; j++) {
                claims[i][j] = scanner.nextInt();
            }
            for (int j = 0; j < 3; j++) {
                allocations[i][j] = scanner.nextInt();
                needs[i][j] = claims[i][j] - allocations[i][j]; // 计算需求
            }
        }

        // 计算当前可用资源
        int[] available = new int[3];
        for (int j = 0; j < 3; j++) {
            available[j] = totalResources[j];
            for (int i = 0; i < 4; i++) {
                available[j] -= allocations[i][j]; // 减去已分配的资源
            }
        }

        // 判断是否为安全状态并找到安全序列
        String safeSequence = findSafeSequence(processNames, needs, available);
        System.out.println(safeSequence);
    }

    private static String findSafeSequence(String[] processNames, int[][] needs, int[] available) {
        boolean[] finish = new boolean[4]; // 每个进程是否完成
        int[] safeSequence = new int[4]; // 安全序列
        int index = 0;

        for (int count = 0; count < 4; count++) {
            boolean canAllocate = false;

            for (int p = 0; p < 4; p++) {
                if (!finish[p]) { // 如果进程p尚未完成
                    boolean allResourcesAvailable = true;
                    for (int r = 0; r < 3; r++) {
                        if (needs[p][r] > available[r]) { // 判断需求资源是否满足
                            allResourcesAvailable = false;
                            break;
                        }
                    }
                    // 如果所有需求资源都可用
                    if (allResourcesAvailable) {
                        // 资源分配
                        for (int r = 0; r < 3; r++) {
                            available[r] += allocations[p][r]; // 释放已分配资源
                        }
                        finish[p] = true; // 标记进程p完成
                        safeSequence[index++] = p; // 记录安全序列
                        canAllocate = true;
                    }
                }
            }
            // 如果没有进程可以分配，则进入不安全状态
            if (!canAllocate) {
                return "false";
            }
        }

        // 构建安全序列字符串
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < safeSequence.length; i++) {
            result.append(processNames[safeSequence[i]]);
            if (i < safeSequence.length - 1) {
                result.append(" ");
            }
        }
        return result.toString();
    }
}